package com.itravel.cms.base;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.itravel.cms.utils.CommonEnum;


public class QueryBase {

	public int pageindex = 0;
	
	public int pagesize = 0;
	
	private int startindex = 0;
	
	public String queryparam; 
	
	private String orderby;
	
	public QueryBase(){};
	
	public QueryBase(int pageindex,int pagesize,Map<String, Integer> orderbyMap,String queryParam){
		this.pageindex = pageindex;
		this.pagesize = pagesize;
		if (orderbyMap != null) {
			setOrderbyMap(orderbyMap);
		}else {
			orderby="";
		}
		
		this.queryparam = queryParam;
		this.startindex = (pageindex-1)*pagesize+1;
	}
	

	public String getOrderby() {
		return orderby;
	}

	public void setOrderbyMap(Map<String, Integer> orderbyMap) {
		this.orderby = getOrderByString(orderbyMap);
	}

	public int getStartindex() {
	
		this.startindex = (pageindex-1)*pagesize;
		return startindex;
	}
	
	private String getOrderByString(Map<String, Integer> map){
		Set<String> mapSet =  map.keySet();	//获取所有的key值 为set的集合
		Iterator<String> itor =  mapSet.iterator();//获取key的Iterator便利
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("order by ");
		int tag = 0;//记录是否有排序字段
		while(itor.hasNext()){//存在下一个值
			String key = itor.next();//当前key值
			int value = -1;
			value = map.get(key);
			
			if(value>-1){
				tag++;
				sBuilder.append(key);
				sBuilder.append(value==CommonEnum.ORDERBY_ASC?" asc ":" desc ");
				sBuilder.append(",");
				
			}
		}
		return tag>0?sBuilder.toString().substring(0,sBuilder.toString().length()-2)
				:"";
	}

	public String getQueryparam() {
		return queryparam;
	}

	public void setQueryparam(String queryParam) {
		queryparam = queryParam;
	}

	public int getPageindex() {
		return pageindex;
	}

	public void setPageindex(int pageIndex) {
		pageindex = pageIndex;
		
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pageSize) {
		pagesize = pageSize;
	}

	public void setStartindex(int startindex) {
		this.startindex = startindex;
	}

	public void setOrderby(String orderBy) {
		orderby = orderBy;
	}
	
	
	
	
	

}
